package com.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.domain.Board;
import com.shixun.domain.BusinessException;
import com.shixun.mapper.BoardMapper;
import com.shixun.service.BoardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Lin
* @description 针对表【board】的数据库操作Service实现
* @createDate 2023-06-16 09:27:53
*/
@Service
public class BoardServiceImpl extends ServiceImpl<BoardMapper, Board>
    implements BoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public void deleteBoard(Integer id) {
        boardMapper.deleteById(id);
    }

    @Override
    public void saveBoard(Board board) throws BusinessException {
        //判断账号是否重复
        LambdaQueryWrapper<Board> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Board::getId,board.getId());
        Long count = Long.valueOf(boardMapper.selectCount(wrapper));
        if (count>0){
            throw new BusinessException("Id已存在，添加失败");
        }
        //判断邮箱是否重复
        wrapper=new LambdaQueryWrapper();
        wrapper.eq(Board::getMessage,board.getMessage());
        count= Long.valueOf(boardMapper.selectCount(wrapper));
        if (count>0){
            throw new BusinessException("公告已存在，添加失败");
        }
        boardMapper.insert(board);
    }

    @Override
    public List<Board> queryBoards() {
        List<Board> boards=boardMapper.selectList(null);
        return boards;
    }

    @Override
    public Board queryById(Integer id) {
        return boardMapper.selectById(id);
    }

    @Override
    public void updateBoard(Board board) {
        boardMapper.updateById(board);
    }
}




